//
//  DataDetailsController.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/31.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "DataDetailsController.h"
#import "ModelForData.h"
#import "NetworkingHandlerOfBlock.h"
#import "CellForData.h"
#import "DataView.h"
#import "ModelForCBA.h"
#import "CellForCBA.h"
#import "CBAView.h"
#import "ModelForTennis.h"
#import "CellForTennis.h"
#import "TennisView.h"
#import <DKNightVersion.h>
#import "CellForShoot.h"
#import "ModelForShoot.h"
#import "NetworkingHandle.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface DataDetailsController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIView *viewForNail;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *arrayForShoot;
@property (nonatomic, retain) NSMutableArray *arrayForZhugong;

@end



@implementation DataDetailsController

- (void)dealloc {
    
    [_viewForNail release];
    [_button release];
    [_tableView release];
    [_array release];
    [_urling release];
    [_urling2 release];
    [_ID release];
    [_arrayForShoot release];
    [_arrayForZhugong release];
    [super dealloc];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"数据";
    [self handleData];
    [self createSubview];
    
    
}

- (void)createSubview {
    
    // tableView
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellForTennis class] forCellReuseIdentifier:@"poolForTennis"];
    [self.tableView registerClass:[CellForCBA class] forCellReuseIdentifier:@"poolForCBA"];
    [self.tableView registerClass:[CellForData class] forCellReuseIdentifier:@"poolForData"];
    [self.tableView registerClass:[CellForShoot class] forCellReuseIdentifier:@"poolForShoot"];
    
    if ([self.ID isEqualToString:@"cba_1"]) {
        CBAView *cbaView = [[CBAView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 18)];
        cbaView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        self.tableView.tableHeaderView = cbaView;
        
        [cbaView release];
        
        
    }else if ([self.ID isEqualToString:@"tennis,tennis_0,tennis_1,tennis_2,tennis_3,tennis_4,tennis_5"]) {
        
        TennisView *tennisView = [[TennisView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 18)];
        tennisView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        self.tableView.tableHeaderView = tennisView;
        [tennisView release];
        
    }else {
        DataView *dataView = [[DataView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 18)];
        dataView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        self.tableView.tableHeaderView = dataView;
        [dataView release];
    }
    
    

    
    
    // NSLog(@"%@", self.urling);
    // webView
    //    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    //    [self.view addSubview:self.webView];
    //    [_webView release];
    //
    //    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]];
    //
    //    [self.webView loadRequest:request];
    //    [request release];
    
}

- (void)buttonAction {
    
    [self.navigationController popViewControllerAnimated:YES];
    //当点击 button 的时候让顶部的 view 和 button 隐藏同时进入上一个 vc
    self.viewForNail.hidden = YES;
    self.button.hidden = YES;
    
}

- (void)handleData {
    
    
    if ([self.ID isEqualToString:@"cba_1"]) {
        
        [NetworkingHandler networkingWithUrl:self.urling completeHandle:^(NSURLResponse *response, NSData *data, id result) {
            
            self.array = [NSMutableArray array];
            NSDictionary *dic = [result objectForKey:@"result"];
            NSArray *array = [dic objectForKey:@"data"];
            
            for (NSDictionary *dict in array) {
                
                ModelForCBA *model = [[ModelForCBA alloc] init];
                
                [model setValuesForKeysWithDictionary:dict];
                [self.array addObject:model];
                [model release];
            }
            
            [self.tableView reloadData];
            
        }];
        
    } else if ([self.ID isEqualToString:@"tennis,tennis_0,tennis_1,tennis_2,tennis_3,tennis_4,tennis_5"]) {
        
        [NetworkingHandle handleWorkData:self.urling AndBody:nil value:nil handleBlock:^(id result) {
            //NSLog(@"%@", self.urling);
            self.array = [NSMutableArray array];
            NSDictionary *dic = [result objectForKey:@"result"];
            NSDictionary *dict = [dic objectForKey:@"data"];
            NSArray *array = [dict objectForKey:@"data"];
            for (NSDictionary *dicttt in array) {
                
                ModelForTennis *model = [[ModelForTennis alloc] init];
                [model setValuesForKeysWithDictionary:dicttt];
                [self.array addObject:model];
                [model release];
            }
            
            [self.tableView reloadData];
        }];
        

    }else {
        
        [NetworkingHandle handleWorkData:self.urling AndBody:nil value:nil handleBlock:^(id result) {
           // NSLog(@"%@", self.urling);
            self.array = [NSMutableArray array];
            NSDictionary *dic = [result objectForKey:@"result"];
            NSDictionary *dict = [dic objectForKey:@"data"];
            NSArray *array = [dict objectForKey:@""];
            
            for (NSDictionary *dicd in array) {
                ModelForData *model = [[ModelForData alloc] init];
                [model setValuesForKeysWithDictionary:dicd];
                [self.array addObject:model];
                [model release];
                
            }
            
            [self.tableView reloadData];

            
        }];
        

        [NetworkingHandle handleWorkData:self.urlForShoot AndBody:nil value:nil handleBlock:^(id result) {
            
                       self.arrayForShoot = [NSMutableArray array];
            
            NSDictionary *dic = [result objectForKey:@"result"];
            NSArray *array = [dic objectForKey:@"data"];
            for (NSDictionary *dict in array) {
                
                ModelForShoot *model = [[ModelForShoot alloc] init];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.arrayForShoot addObject:model];
                
                [model release];
                
            }
            
            [self.tableView reloadData];
        }];
        

        [NetworkingHandle handleWorkData:self.urlForzhugong AndBody:nil value:nil handleBlock:^(id result) {
            
            //NSLog(@"%@", self.urlForzhugong);
            self.arrayForZhugong= [NSMutableArray array];
            
            NSDictionary *dic = [result objectForKey:@"result"];
            NSArray *array = [dic objectForKey:@"data"];
            for (NSDictionary *dict in array) {
                
                ModelForShoot *model = [[ModelForShoot alloc] init];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.arrayForZhugong addObject:model];
                
                [model release];
                
            }
            
            [self.tableView reloadData];
        }];
        
        
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
    }else {
        
        if (self.arrayForShoot != nil) {
            
            return 40 * HEIGHTT;
        } else {
            
            return 0;
            
        }
        
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (self.arrayForShoot != nil) {
        
    
        if (section == 1) {
            
            UIView *shoot = [[UIView alloc] init];
            
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHTT)];
            image.image = [UIImage imageNamed:@"shoot"];
            
            [shoot addSubview:image];
            [image release];
            return shoot;
            
        }  else  if (section == 2){
            
            
            UIView *shoot = [[UIView alloc] init];
            
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHTT)];
            image.image = [UIImage imageNamed:@"zhugong"];
            
            [shoot addSubview:image];
            [image release];
            return shoot;
            
        } else {
            
            return 0;
            
        }
    }  else {
        
        return 0;
        
    }
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return  self.array.count;
    } else {
        
        if (self.arrayForShoot != nil) {
            
            return 5;
        } else {
            
            return 0;
            
        }
        
        
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        
        if ([self.ID isEqualToString:@"cba_1"]) {
            
            CellForCBA *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCBA"];
            
            ModelForCBA *model = [self.array objectAtIndex:indexPath.row];
            
            [cell passModelForData:model];
            
            return cell;
            
        } else if ([self.ID isEqualToString:@"tennis,tennis_0,tennis_1,tennis_2,tennis_3,tennis_4,tennis_5"]) {
            
            CellForTennis *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForTennis"];
            ModelForTennis *model = [self.array objectAtIndex:indexPath.row];
            
            [cell passModelForTennis:model];
            
            return cell;
            
        } else {
            
            CellForData *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForData"];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            ModelForData *model = [self.array objectAtIndex:indexPath.row];
            [cell passModelForData:model];
            
            return cell;
        }
        
    }else if (indexPath.section == 0){
        
        CellForShoot *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForShoot"];
        
        ModelForShoot *model = [self.arrayForShoot objectAtIndex:indexPath.row];
        
        [cell passModelForShoot:model];
        
        
        return cell;
        
        
    } else {
        
        
#warning 中超数据不正常 抓包重取中超的球队数据
        
        CellForShoot *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForShoot"];
        
        ModelForShoot *model = [self.arrayForZhugong objectAtIndex:indexPath.row];
        
        
        [cell passModelForShoot:model];
        
        
        return cell;
        
        
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return  self.view.frame.size.height / 12;
    }else {
        
        return self.view.frame.size.height / 8;
        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
